# Maintainer: Sven-Hendrik Haase <svenstaro@gmail.com>
# Maintainer: Maxim Baz <archlinux at maximbaz dot com>
# Contributor: Andrew Gallant <jamslam@gmail.com>
pkgname=ripgrep
pkgver=13.0.0
pkgrel=3
pkgdesc="A search tool that combines the usability of ag with the raw speed of grep"
arch=('loong64' 'x86_64')
url="https://github.com/BurntSushi/ripgrep"
license=('MIT' 'custom')
depends=('gcc-libs' 'pcre2')
makedepends=('cargo' 'asciidoctor')
source=("$pkgname-$pkgver.tar.gz::https://github.com/BurntSushi/$pkgname/archive/$pkgver.tar.gz")
sha512sums=('9321532e4bf633ecd200d98873b6773230d046d7bd075f223f09a68531cef4e4138f01c6b41e0f8697805963ae7e0d44d542c6d94025d9a06fbbef3562c17734')

build() {
  cd "$pkgname-$pkgver"
  find -name Cargo.lock -exec rm -f {} \;
  mkdir -p .cargo
  cat > .cargo/config.toml <<EOF
[source.crates-io]
registry = "https://gitee.com/yetist/crates.io-index"
EOF

  cargo build --release --features 'pcre2'
  find target -name ripgrep-stamp -print0 | xargs -0 ls -t | head -n1 | xargs dirname > out_dir
}

check() {
  cd "$pkgname-$pkgver"

  cargo test --release --locked --features 'pcre2'
}

package() {
  cd "$pkgname-$pkgver"
  local OUT_DIR=$(<out_dir)

  install -Dm755 "target/release/rg" "$pkgdir/usr/bin/rg"

  install -Dm644 "complete/_rg" "$pkgdir/usr/share/zsh/site-functions/_rg"
  install -Dm644 "$OUT_DIR"/rg.bash "$pkgdir/usr/share/bash-completion/completions/rg"
  install -Dm644 "$OUT_DIR"/rg.fish "$pkgdir/usr/share/fish/vendor_completions.d/rg.fish"
  install -Dm644 "$OUT_DIR"/rg.1 "$pkgdir/usr/share/man/man1/rg.1"

  install -Dm644 "README.md" "$pkgdir/usr/share/doc/${pkgname}/README.md"
  install -Dm644 "COPYING" "$pkgdir/usr/share/licenses/${pkgname}/COPYING"
  install -Dm644 "LICENSE-MIT" "$pkgdir/usr/share/licenses/${pkgname}/LICENSE-MIT"
  install -Dm644 "UNLICENSE" "$pkgdir/usr/share/licenses/${pkgname}/UNLICENSE"
}
